package com.group.nonefornow;

//import java.awt.Color;
//import java.lang.reflect.Field;

import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

public class Game extends BasicGame {
	
	public Game() {
		super("NoneForNow Game");
	}
	
//	public org.newdawn.slick.Color getColorByName(String colorName) {
//		Color c = null;
//		try {
//			Field f = Class.forName("java.awt.Color").getField(colorName.toLowerCase());
//			c = (Color) f.get(null);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		
//		return (c == null) ? Color.RED : c;
//	}

	@Override
	public void init(GameContainer gc) throws SlickException {
		BallContainer.getInstance().addBall(new Ball(50, 50, 20, 20));
	}
	
	@Override
	public void mouseClicked(int button, int x, int y, int clickCount) {
		if (button == 0) {
			BallContainer.getInstance().addBall(new Ball(x, y, 20, 20));
		}
	}
	
	@Override
	public void update(GameContainer gc, int delta) throws SlickException {
		BallContainer container = BallContainer.getInstance();
		for (int i = 0; i < container.size(); i++) {
			Ball current = container.get(i);
			if (current.getX() + current.getWidth() > gc.getWidth() ||
					current.getX() - current.getWidth() < current.getWidth()*-1 ||
					current.getY() + current.getHeight() > gc.getHeight() ||
					current.getY() - current.getHeight() < current.getHeight()*-1) {
				current.setDoesStop(true);
			}
			else {
				for (int j = 0; j < container.size(); j++) {
					Ball counter = container.get(j);
					if (current.getRect().intersects(counter.getRect()) && current != counter) {
						current.setDoesStop(true);
						break;
					}
				}
				if (!current.doesStop()) {
					//current.setX(current.getX() + 1);
					current.setY(current.getY() + 1);
				} 
			}
		}
	}
	
	@Override
	public void render(GameContainer gc, Graphics g) throws SlickException {
		g.clear();
		BallContainer container = BallContainer.getInstance();
		g.drawString("Christmas cubes spawned: " + container.size(), 10, 50);
		for (int i = 0; i < container.size(); i++) {
			Ball current = container.get(i);
			g.setColor(Color.red);
			g.fillRect(current.getX(), current.getY(), current.getWidth(), current.getHeight());
			g.setColor(Color.green);
			g.fillOval(current.getX(), current.getY(), current.getWidth(), current.getHeight());
		}
	}
}
